#include<iostream>
#include<stdio.h>
//#include<cstdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include <thread>
#include<string>
#include <mutex>
#include<netdb.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<cstring>
#include<netinet/in.h>

using namespace std;

int main(int argc,char **argv){

    char serip[] = "127.0.0.1";
    int port = 3333;
   sockaddr_in seraddr;

   char buf[1024];
   int buflen;



    seraddr.sin_family = AF_INET;
    seraddr.sin_port = htons(port);
    seraddr.sin_addr.s_addr = inet_addr(serip);

   buflen = sizeof(buf);
   memset(&buf,0,buflen);

   int sfd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
   if(connect(sfd, (sockaddr *)&seraddr, sizeof(seraddr)) !=0){
    perror("connect fail");
    exit(1);
   };

   recv(sfd, &buf,sizeof(buf),0);
   printf("客户端收到信息：%s",buf);

   for (size_t i = 0; i < 3; i++)
   {
    bzero(&buf,buflen);
    sprintf(buf,"%s %ld","client message ",i);
    send(sfd,&buf,buflen, 0);
    
    bzero(&buf,buflen);
    recv(sfd,&buf,buflen,0);
    cout<<"客户端收到的信息："<<buf<<endl;
    

   }
   close(sfd);



   return 0;
}